package com.eboss2.sbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.model.MemberCouponEnitity;
import com.mytools.tool.utils.StringUtil;

import java.util.BitSet;
import java.util.List;

public class MemberCouponListAdapter extends BaseAdapter {
	
	private Context context;
	private BitSet bitSet;
	private List<MemberCouponEnitity> datas;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	private MemberCounponCheckCallback callBack;

	public MemberCouponListAdapter(Context context, List<MemberCouponEnitity> datas, BitSet bitSet, MemberCounponCheckCallback callBack) {
		this.context = context;
		this.datas = datas;
		this.bitSet = bitSet;
		this.callBack = callBack;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0)return datas.size();
		else return 0;
	}

	@Override
	public MemberCouponEnitity getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MemberCouponEnitity data = datas.get(position);
		
		inflater = LayoutInflater.from(context);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.listitem_member_coupon, null);

			holder.ll_list_item = (LinearLayout) convertView.findViewById(R.id.ll_list_item);
			holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_check = (LinearLayout) convertView.findViewById(R.id.tv_check);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_money.setText(StringUtil.getMoneyString(data.getMoney()) +"元券");
		holder.tv_name.setText((data.getCanMultiChoose() == 1?"(复)":"(单)")+data.getName());
		holder.tv_check.setSelected(bitSet.get(position));
		holder.ll_list_item.setOnClickListener(new MemberCouponChoose(position));
		return convertView;
	}
	
	class MemberCouponChoose implements OnClickListener{
		private int position;
		MemberCouponChoose(int position){
			this.position = position;
		}
		@Override
		public void onClick(View v) {
			callBack.callback(position);
		}
	}

	public final class ViewHolder {
		private TextView tv_money;
		private TextView tv_name;
		private LinearLayout tv_check;
		private LinearLayout ll_list_item;
	}
	
	public interface MemberCounponCheckCallback {
		public void callback(int position);
	}

}
